package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SalChance;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISalChanceService;
import com.czkt.service.SysUserService;
import com.czkt.util.PageUtile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liu
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/chance")
public class SalChanceController {


    @Autowired
    ISalChanceService salChanceService;
    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/list")
    public Object getChanceList(SalChance salChance, @RequestParam(defaultValue = "1") Integer pageIndex,
                                @RequestParam(defaultValue = "2") Integer pageSize, Model model) {

        IPage<SalChance> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);

        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();


        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(salChance.getChcCustName()), SalChance::getChcCustName, salChance.getChcCustName())
                .like(StringUtils.hasText(salChance.getChcDesc()), SalChance::getChcDesc, salChance.getChcDesc())
                .eq(currentUser.getRole().getRoleId() == 2, SalChance::getChcDueId, currentUser.getUsrId())
        ;
        page = salChanceService.page(page, queryWrapper);

        PageUtile pageUtile = new PageUtile();

        BeanUtils.copyProperties(page, pageUtile);
        model.addAttribute("chancePage", pageUtile);
        model.addAttribute("custName", salChance.getChcCustName());
        model.addAttribute("chcDesc", salChance.getChcDesc());

        return "chance/list";
    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/add")
    public Object toAdd(Model model) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.lambda().eq(SysUser::getUsrRoleId, 2)
                .or().eq(SysUser::getUsrRoleId, 14);
        model.addAttribute("users", sysUserService.list(queryWrapper));
        return "chance/add";
    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/save")
    @Transactional
    public Object saveOrUpdate(SalChance salChance, Model model) {

        boolean result = salChanceService.saveOrUpdate(salChance);
        if (result) {


            return "redirect:/chance/list";
        } else if (salChance.getChcId() == null) {
            return "redirect:/chance/add";
        } else {
            return "redirect:/chance/edit?usrId=" + salChance.getChcId();
        }

    }


    /**
     * 定义跳转到修改页的方法
     */
    @RequestMapping("/edit")
    public Object toEdit(long chcId, Model model) {
        model.addAttribute("chance", salChanceService.getById(chcId));

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.lambda().eq(SysUser::getUsrRoleId, 2)
                .or().eq(SysUser::getUsrRoleId, 14);
        model.addAttribute("users", sysUserService.list(queryWrapper));

        return "chance/edit";
    }


    /**
     * 定义跳转到修改页的方法
     */
    @RequestMapping("/del/{chcId}")
    @ResponseBody
    public Object del(@PathVariable long chcId, Model model) {
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SalChance::getChcId ,chcId)
                .isNotNull(SalChance::getChcStatus);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("delResult",salChanceService.remove(queryWrapper));
        return resultMap;

    }


}
